package cn.hexcloud.dga.meta.service.impl;

import cn.hexcloud.dga.meta.bean.TableMetaInfo;
import cn.hexcloud.dga.meta.bean.TableMetaInfoExtra;
import cn.hexcloud.dga.common.constant.MetaConst;
import cn.hexcloud.dga.meta.mapper.TableMetaInfoExtraMapper;
import cn.hexcloud.dga.meta.service.TableMetaInfoExtraService;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 元数据表附加信息 服务实现类
 * </p>
 *
 * @author jiangdan7
 * @since 2023-08-24
 */
@Service
@DS("dga")
public class TableMetaInfoExtraServiceImpl extends ServiceImpl<TableMetaInfoExtraMapper, TableMetaInfoExtra> implements TableMetaInfoExtraService {

    /**
     * 根据元数据表列表生成
     *
     * @param tableMetaInfoList 元数据表列表
     */
    @Override
    public void initTableMetaExtra(List<TableMetaInfo> tableMetaInfoList) {
        //1 检查 要生成哪些辅助信息表  只初始化没生成的tableMetaInfoList
        List<String> metaInfoNameList = tableMetaInfoList.stream().map(a -> a.getSchemaName() + "." + a.getTableName()).collect(Collectors.toList());
        Set<String> extraNameInfo = this.list(new QueryWrapper<TableMetaInfoExtra>().in("concat(schema_name,'.',table_name)", metaInfoNameList))
                .stream().map(a -> a.getSchemaName() + "." + a.getTableName()).collect(Collectors.toSet());
        tableMetaInfoList.removeIf(tableMetaInfo -> extraNameInfo.contains(tableMetaInfo.getSchemaName() + "." + tableMetaInfo.getTableName()));
        //2  初始化 没有曾经初始化过的表
        ArrayList<TableMetaInfoExtra> metaInfoExtras = new ArrayList<>(tableMetaInfoList.size());
        for (TableMetaInfo tableMetaInfo : tableMetaInfoList) {
            TableMetaInfoExtra tableMetaInfoExtra = TableMetaInfoExtra.builder()
                    .tableName(tableMetaInfo.getTableName())
                    .schemaName(tableMetaInfo.getSchemaName())
                    .lifecycleType(MetaConst.LIFECYCLE_TYPE_UNSET)
                    .lifecycleDays(-1L)
                    .securityLevel(MetaConst.SECURITY_LEVEL_UNSET)
                    .dwLevel(getInitDwLevelByTableName(tableMetaInfo.getTableName()))
                    .createTime(new Date())
                    .build();
            metaInfoExtras.add(tableMetaInfoExtra);
        }
        this.saveBatch(metaInfoExtras,500);
    }

    private String getInitDwLevelByTableName(String tableName) {
        if(tableName.startsWith("ods")){
            return "ODS";
        }else if(tableName.startsWith("dwd")){
            return "DWD";
        }else if(tableName.startsWith("dws")){
            return "DWS";
        }else if(tableName.startsWith("ads")){
            return "ADS";
        }else if(tableName.startsWith("dim")){
            return "DIM";
        }else if(tableName.startsWith("dm")){
            return "DM";
        }else{
            return "OTHER";
        }
    }

    @Override
    public void saveTableMetaInfoExtra(TableMetaInfoExtra tableMetaInfoExtra) {
        tableMetaInfoExtra.setUpdateTime(new Date());
        update(tableMetaInfoExtra,new QueryWrapper<TableMetaInfoExtra>().eq("schema_name",tableMetaInfoExtra.getSchemaName()).eq("table_name",tableMetaInfoExtra.getTableName()));
    }
}
